package com.powerdata.core.paimon.catalog;

import org.apache.avro.file.DataFileReader;
import org.apache.avro.generic.GenericDatumReader;
import org.apache.avro.generic.GenericRecord;
import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.fs.AvroFSInput;
import org.apache.hadoop.fs.FileContext;
import org.apache.hadoop.fs.Path;

public class PDHdfsAvroUtils {


    public static String catAvroFile(String FilePath) throws Exception {
        StringBuilder result = new StringBuilder();
        Path path = new Path(FilePath);
        DataFileReader<GenericRecord> reader =
                new DataFileReader<>(new AvroFSInput(FileContext.getFileContext(), path), new GenericDatumReader<>());
        GenericRecord record = reader.next();
        while (record != null) {
            result.append("\n").append(record).append(",");
            try {
                record = reader.next();
            } catch (Exception e) {
                break;
            }
        }
        reader.close();
        if (StringUtils.isEmpty(result.toString())) {
            return result.toString();
        }
        return "[" + result.substring(0, result.length() - 1) + "]";
    }

}
